# 生成优惠卷脚本

import random
import string
import pymysql


# 连接数据库
conn = pymysql.connect(host='localhost',user='root',password='',db='deu',charset='utf8')


# 定义随机串元素
code = string.ascii_letters + string.digits

# 定义四位随机码
def getCode():
    return "".join(random.sample(code,4))


# 定义四列四位随机码
def key(group):
    return "-".join([getCode() for i in range(group)])


mycode = key(4)

# 创建游标对象
cursor = conn.cursor()

# 定义sql语句
sql = "insert into codes (code,discount) values ('%s',%s)" % (mycode, '0.8')

# 入库操作
cursor.execute(sql)
# 提交事务
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()


